package com.cqupt.command.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cqupt.command.Command;
import com.cqupt.utils.FileReadMan;
import com.cqupt.utils.Result;

public class GrepCommand implements Command{
	// 选项列表
			private List<String> options;
			// 参数列表..
			private String parameters;
			// 上命令的结果
			private String result;

			public GrepCommand(String command, String result) {
				// 命令解析得到选项
				this.options = getOptionsAndParameter(command);
				// 命令解析得到参数
				this.parameters = getParameters(command);
				// 得到上个命令的结果
				this.result = result;
			}

//			private List<String> getOptions(String command) {
//				List<String> result = new ArrayList<>();
//				Pattern pattern = Pattern.compile("-[a-z,A-Z]+");
//				Matcher matcher = pattern.matcher(command);
//				while (matcher.find()) {
//					String parameterA = matcher.group();
//					char[] chars = parameterA.substring(1, parameterA.length()).toCharArray();
//					for (int i = 0; i < chars.length; i++) {
//						result.add(chars[i] + "");
//					}
//				}
//				return result;
//			}
			
			private List<String> getOptionsAndParameter(String command) {
				List<String> result = new ArrayList<>();
				Pattern pattern = Pattern.compile("-[a-z,A-Z]+");
				Matcher matcher = pattern.matcher(command);
				while (matcher.find()) {
					String parameterA = matcher.group();
					char[] chars = parameterA.substring(1, parameterA.length()).toCharArray();
					for (int i = 0; i < chars.length; i++) {
						result.add(chars[i] + "");
					}
				}
				String[] cmd = command.split(" ");
				for(int i=0;i<cmd.length;i++){
					String tempParameter = cmd[i].trim();
					if (tempParameter.indexOf("-")==-1 || tempParameter.indexOf("grep")==-1){
						result.add(tempParameter + "");
					}
				}
				return result;
			}

			private String getParameters(String command) {
				String[] cmd = command.split(" ");
				return cmd[cmd.length - 1].trim();
			}

			
			//-c 显示有多少行被匹配到，    -A 找到所有的匹配行，并显示匹配行后面N行 
			@Override
			public String run() {
				// 确定选项
				boolean displayNum = false;// 显示行号
				boolean noAppear = false;// 匹配字符没有出现的行  
				boolean appear = true;// 匹配字符出现的行
				String tempString = "";//判断是否包含该字符
				for (int i = 0; i < options.size(); i++) {
					String option = options.get(i);					
					switch (option) {
					case "n":
						displayNum = true;
						continue;
					case "v":
						noAppear = true;
						appear = false;
						break;
					case "":
						continue;
					default:
						tempString = option.trim();
						break;
					}
				}
				// 获取数据源
				List<String> listString = new ArrayList<>();
				if (result != null) {
					String[] resultSource = result.split("\r?\n");
					for (int i = 0; i < resultSource.length; i++) {
						String sourceLine = resultSource[i];
						String tempLine = "";
						if (displayNum) {
							tempLine = tempLine + (i+1) +"："+"\t";		
						}
						if("".equals(tempString)){
							tempLine = tempLine + sourceLine;
							listString.add(tempLine);
						}else{
							if (noAppear && sourceLine.indexOf(tempString)==-1){
								tempLine = tempLine + sourceLine;
								listString.add(tempLine);
							}else if (appear && sourceLine.indexOf(tempString)!=-1){
								tempLine = tempLine + sourceLine;
								listString.add(tempLine);
							}
						}
					}
				} else {
					FileReadMan fileReadMan = new FileReadMan(parameters);
					int tempNum = 1;
					while (fileReadMan.hasNext()) {
						String sourceLine = fileReadMan.next();
						String tempLine = "";
						if (displayNum) {
							tempLine = tempLine + tempNum +"："+"\t";	
							tempNum++;
						}
						if("".equals(tempString)){
							tempLine = tempLine + sourceLine;
							listString.add(tempLine);
						}else{
							if (noAppear && sourceLine.indexOf(tempString)==-1){
								tempLine = tempLine + sourceLine;
								listString.add(tempLine);
							}else if (appear && sourceLine.indexOf(tempString)!=-1){
								tempLine = tempLine + sourceLine;
								listString.add(tempLine);
							}
						}
					}
				}
				Result<String> result = new Result<String>();
				return result.getResult(listString);
			}
}
